import { defineStore } from 'pinia'
import { Names } from './store-name'

// 系统全局配置
export const useSystemConfigStore = defineStore(Names.systemConfig, {
    state: () => ({
        theme: 'null' as 'null' | 'dark',
        // 展示页面
        page: 'localPlayList',
        // 设置页面
        setPage: 'systemConfig',
        load: false,
        loadDescription: '加载中',
        loadStroke: 'pink',
        // 菜单是否折叠
        collapsed: false,
        // 定时暂停
        countPause: 0,
        // 定时关机
        countDown: 0,
        // 展示乐库主页
        showAudioLibraryHall: true,
        musicListFontColorStrat: '',
        musicListFontColorMiddle: '',
        musicListFontColorEnd: '',
        musicListFontColorDeg: 90,
    }),
    actions: {
        // 更换主题
        changeTheme(value: 'null' | 'dark') {
            this.theme = value
        },
        // 切换主页
        changePage(value: string) {
            this.page = value
        },
        // 切换设置页
        changeSetPage(value: string) {
            this.setPage = value
        },
        // 是否显示加载
        changeLoading(flag: boolean, value: string) {
            this.load = flag
            this.loadDescription = value
        },
        // 加载时背景颜色
        changeloadStroke(value: string) {
            this.loadStroke = value
        },
        // 菜单是否折叠
        changeCollapsed(value: boolean) {
            this.collapsed = value
        },
        // 倒计时暂停
        changeCountPause(value: number) {
            this.countPause = value
        },
        // 倒计时关闭
        changeCountDown(value: number) {
            this.countDown = value
        },
        // 改变搜索歌曲、歌词或歌手的值
        changeShowAudioLibraryHall(value: boolean) {
            this.showAudioLibraryHall = value
        },
        // 变更音乐列表文字渐变初始颜色
        changeMusicListFontColorStrat(value: string) {
            this.musicListFontColorStrat = value
        },
        // 变更音乐列表文字渐变中间颜色
        changeMusicListFontColorMiddle(value: string) {
            this.musicListFontColorMiddle = value
        },
        // 变更音乐列表文字渐变结束颜色
        changeMusicListFontColorEnd(value: string) {
            this.musicListFontColorEnd = value
        },
        // 变更音乐列表文字渐变距离
        changeMusicListFontColorDeg(value: number) {
            this.musicListFontColorDeg = value
        }
    },
    persist: {
        enabled: true,
        strategies: [
            { storage: localStorage, paths: ['theme', 'page', 'setPage', 'load', 'loadDescription', 'loadStroke', 'collapsed', 'countPause', 'countDown', 'showAudioLibraryHall', 'musicListFontColorStrat', 'musicListFontColorMiddle', 'musicListFontColorEnd', 'musicListFontColorDeg'] },
        ]
    }
})

// 用户信息
export const userInfoStore = defineStore(Names.userInfo, {
    state: () => ({
        uid: '',
        username: '',
        roles: '',
        joinDate: 0,
        loginDate: 0,
        level: 0,
        cloundSize: 0,
        useCloundSize: 0,
        cloundAuth: false,
        ip: '',
        client: '',
        token: '',
        loginState: false
    }),
    actions: {
        // 修改用户信息
        changeUserInfo(_id: string, username: string, roles: string, joinDate: number, level: number, cloundSize: number, useCloundSize: number, cloundAuth: boolean) {
            this.uid = _id
            this.username = username
            this.roles = roles
            this.joinDate = joinDate
            this.level = level
            this.cloundSize = cloundSize
            this.useCloundSize = useCloundSize
            this.cloundAuth = cloundAuth
        },
        // 存储token
        changeUserToken(token: string, loginState: boolean) {
            this.token = token
            this.loginState = loginState
        },
        // 修改用户设备信息
        changeUserClientAndIp(ip: string, loginDate: number, client: string,) {
            this.ip = ip
            this.client = client
            this.loginDate = loginDate
        },
        // 清除用户信息
        clearUserInfo() {
            this.uid = ''
            this.username = ''
            this.roles = ''
            this.joinDate = 0
            this.loginDate = 0
            this.level = 0
            this.cloundSize = 0
            this.useCloundSize = 0
            this.ip = ''
            this.client = ''
            this.token = ''
            this.loginState = false
        }
    },
    persist: {
        enabled: true,
        strategies: [
            { storage: localStorage, paths: ['uid', 'username', 'roles', 'joinDate', 'loginDate', 'level', 'cloundSize', 'useCloundSize', 'cloundAuth', 'ip', 'client', 'token', 'loginState'] },
        ]
    }
})

// 播放音频的配置
export const usePlayConfigStore = defineStore(Names.playConfig, {
    state: (): PlayConfigState => ({
        index: 0,
        audioId: '',
        audioName: '',
        audioSrc: '',
        audioCover: '',
        voice: 0.5,
        position: 0,
        rate: 1,
        mute: false,
        autoPlay: false,
        playTime: 0,
        playMode: "order",
        playListMode: "localPlayList"
    }),
    actions: {
        changeIndex(value: number): void {
            this.index = value
        },
        changeAudioId(value: string): void {
            this.audioId = value
        },
        changeAudioName(value: string): void {
            this.audioName = value
        },
        changeAudioSrc(value: string): void {
            this.audioSrc = value
        },
        changeAudioCover(value: string): void {
            this.audioCover = value
        },
        changeVoice(value: number): void {
            this.voice = value
        },
        changePosition(value: number): void {
            this.position = value
        },
        changeRate(value: PlayConfigState['rate']): void {
            this.rate = value
        },
        changeMute(value: boolean): void {
            this.mute = value
        },
        changeAutoPlay(value: boolean): void {
            this.autoPlay = value
        },
        changePlayTime(value: number): void {
            this.playTime = value
        },
        changePlayMode(value: PlayConfigState['playMode']): void {
            this.playMode = value
        },
        changePlayListMode(value: PlayConfigState['playListMode']): void {
            this.playListMode = value
        }
    },
    persist: {
        enabled: true,
        strategies: [
            { storage: localStorage, paths: ['index', 'audioId', 'audioName', 'audioSrc', 'audioCover', 'voice', 'position', 'rate', 'mute', 'autoPlay', 'playTime', 'playMode', 'playListMode'] },
        ]
    }
})

// 播放列表
export const usePlayListStore = defineStore(Names.playList, {
    state: () => ({
        audioLibrary: [] as PlayListState[],
        audioLibraryPage: 1,
        audioLibrarySearchCount: 0,
        likePlayList: [] as PlayListState[],
        collectPlayList: [] as PlayListState[],
        localPlayList: [] as PlayListState[],
    }),
    actions: {
        // 重置乐库列表
        resetAudioLibraryList() {
            this.audioLibrary = []
            this.audioLibrarySearchCount = 0
        },
        // 重置我的喜欢列表
        resetLikePlayList() { 
            this.likePlayList = []
        },
        // 导入本地列表
        importLikePlayList(array: PlayListState[]): void {
            this.likePlayList = []
            this.likePlayList = array
        },
        // 导入本地列表
        importLocalPlayList(value: PlayListState): void {

            let flag = true
            // 要做数组去重
            for (let index = 0; index < this.localPlayList.length; index++) {
                const e = this.localPlayList[index];

                if (value.audioName == e.audioName) {
                    flag = false
                    return
                }

            }

            flag ? this.localPlayList.push(value) : ''

        },
        // 删除本地列表
        deleteLocalPlayList(index: number) {
            this.localPlayList.splice(index, 1)
        },
        // 重置本地列表
        resetLocalPlayList() {
            this.localPlayList = []
        },
        // 定位列表音频位置
        positionAudioInPlayList(playList: 'localPlayList' | "likePlayList" | "collectPlayList" | 'cloudPlayList' | 'audioLibrary', index: number) {
            this[playList].map((item: { play: boolean }, i: number) => {
                i == index ? item.play = true : item.play = false
            })
        },
        // 清除定位列表音频位置
        resetPositionAudioInPlayList(playList: 'localPlayList' | "likePlayList" | "collectPlayList" | 'cloudPlayList' | 'audioLibrary') {
            this[playList].map((item: { play: boolean }) => {
                item.play = false
            })
        }

    },
    persist: {
        enabled: true,
        strategies: [
            { storage: localStorage, paths: ['audioLibrary', 'audioLibraryPage', 'audioLibrarySearchCount', 'likePlayList', 'collectPlayList', 'localPlayList'] },
        ]
    }
})